<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" lock-scroll :append-to-body="true" :close-on-click-modal="false"
      width="389px" :close-on-press-escape="false">
      <div class="login-form" v-if="!isSuccess">
        <!-- 修改密码 -->
        <div class="login-title" style="color: #1890fc">修改密码</div>
        <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="loginForm" label-position="left">
          <el-form-item prop="oldPassword">
            <el-input autocomplete="off" ref="oldPassword" v-model="registerForm.oldPassword" placeholder="请输入您的旧密码"
              name="oldPassword" type="text" tabindex="1" class="userinput"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input ref="password" v-model="registerForm.password" :type="rpFlag ? 'password' : 'text'"
              placeholder="请输入新的密码" name="password" tabindex="3" class="userinput pwdinput">
              <img slot="suffix" style="width: 32px; height: 32px; margin-top: 5px;"
                :src="!rpFlag ? require('../../assets/login/lookeye.png') : require('../../assets/login/lookno.png')"
                @click="rpFlag = !rpFlag" />
            </el-input>
          </el-form-item>
          <el-form-item prop="passwordAgain">
            <el-input ref="passwordAgain" v-model="registerForm.passwordAgain" :type="repFlag ? 'password' : 'text'"
              placeholder="请再次输入新的密码" name="passwordAgain" tabindex="4" class="userinput pwdinput">
              <img slot="suffix" style="width: 32px; height: 32px; margin-top: 5px;"
                :src="!repFlag ? require('../../assets/login/lookeye.png') : require('../../assets/login/lookno.png')"
                @click="repFlag = !repFlag" />
            </el-input>
          </el-form-item>
          <el-button :loading="loading" type="primary"
            style="width: 100%; margin-top: 50px; display: inline-block;height: 50px;"
            @click.native.prevent="handleRegister">完&nbsp;&nbsp;成</el-button>
        </el-form>
      </div>
      <div class="login-form" v-else>
        <!-- 修改密码 -->
        <div class="login-title" style="color: #1890fc">修改密码</div>
        <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="loginForm" label-position="left">
          <div class="success-title">您的密码已修改成功</div>
          <el-button :loading="loading" type="primary"
            style="width: 100%; margin-top: 50px; display: inline-block;height: 50px;"
            @click.native.prevent="handleLogin">点击登录</el-button>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Vue from 'vue'
import { USER_INFO, USER_NAME, ACCOUNT_BOOK } from '@/store/mutation-types'
import { postAction, getAction } from '@/api/manage'
import { setToken } from '@/utils/auth'
export default {
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('登录密码不能少于6位!'))
      } else if (value.length > 16) {
        callback(new Error('登录密码不能多于16位!'))
      } else {
        callback()
      }
    }
    const validatePasswordAgain = (rule, value, callback) => {
      if (value != this.registerForm.password) {
        callback(new Error('两次输入密码不一致！'))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      rpFlag: true, // 密码是否可见
      repFlag: true, // 确认密码是否可见
      registerForm: {
        oldPassword: '',
        password: '',
        passwordAgain: '',
      },
      loading: false,
      registerRules: {
        oldPassword: [
          { required: true, message: '请输入6-16位密码!' },
          { trigger: ['blur', 'change'], validator: validatePassword },
        ],
        password: [
          { required: true, message: '请输入6-16位密码!' },
          { trigger: ['blur', 'change'], validator: validatePassword },
        ],
        passwordAgain: [
          { required: true, message: '请输入6-16位密码!' },
          { trigger: ['blur', 'change'], validator: validatePasswordAgain },
        ],
      },
      isSuccess: false,
    }
  },
  methods: {
    handleRegister() {
      const that = this
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          that.loading = true
          postAction('/accountbookinfo/xghAccountBook/changePwd', {
            ...that.registerForm,
            phone: Vue.ls.get(USER_NAME),
          }).then(res => {
            that.loading = false
            if (res.code == 200) {
              that.handleLogin()
            } else {
              that.$elMessage.error(res.message)
            }
          })
        }
      })
    },
    handleLogin() {
      const that = this
      setToken('')
      that.$router.push('/user/Login').catch(err => { })
    },
  },
}
</script>

<style lang="scss" scoped>
.login-form {
  position: relative;
  width: 389px; //7.294206rem;
  height: 474px; //8.888056rem;
  max-width: 100%;
  // margin-top: 1.631352rem;
  // margin-right: 1.875117rem;
  overflow: hidden;
  background: #ffffff;
  // display: flex;
  justify-content: center;
  padding-left: 30px;
  padding-right: 30px;

  .login-title {
    font-size: 20px;
    font-family: PingFang SC;
    font-weight: bold;
    line-height: 28px;
    // color: #1890fc;
    width: 100%;
    display: flex;
    justify-content: center;
    margin-top: 10px;
  }

  .success-title {
    margin-top: 30px;
    line-height: 195px;
    text-align: center;
    font-size: 16px;
    font-family: PingFang SC-Regular, PingFang SC;
    font-weight: 400;
    color: #666666;
  }

  .preimg {
    vertical-align: middle;
    margin-top: 5px;
    width: 17px;
    height: 20px;
    margin-left: 3px;
  }

  .pwdimg {
    margin-left: 4px;
    width: 15px;
  }

  .userinput {
    width: 329px; //6.169136rem;
    height: 50px; //0.937559rem;
    background: #ffffff;
    margin-top: 25px;
    color: #666666;
    font-size: 14px;
  }

  .codeinput {
    margin-top: 23px;
    width: 203px;
  }

  .pwdinput {
    margin-top: 23px;
  }

  .btn-sendmsg {
    margin-left: 14px;
    width: 112px;
    height: 50px;
    background: #1890fc;
    border-radius: 0.093756rem;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #ffffff;
    opacity: 1;
  }

  .agreement-view {
    margin-top: 27px;
    align-items: center;
    display: flex;
    font-size: 12px;

    .readtext {
      font-family: PingFang SC;
      font-weight: 400;
      color: #666666;
      opacity: 1;
      line-height: 21px;
    }

    .agreement {
      font-family: PingFang SC;
      font-weight: 400;
      color: #1993FB;
      opacity: 1;
      line-height: 21px;
    }
  }

  .go-login-tip {
    margin-top: 20px;
    align-items: center;
    display: flex;
    justify-content: center;

    .has-account {
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #707070;
      opacity: 1;
    }

    .go-login {
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #1890fc;
      opacity: 1;
      margin-left: 0.093756rem;
    }
  }
}

::v-deep .el-form-item {
  margin-bottom: 2px !important;
}

::v-deep .el-dialog {
  // margin-top: 33vh !important;
  height: 474px;
  overflow: hidden;

  .el-dialog__body {
    padding: 0;
  }

  .el-dialog__header {
    width: 100%;
    height: 0.937559rem;
    box-sizing: border-box;
    padding: 0 0.375023rem;
    // border-bottom: 1px solid #eeeeee;
    display: flex;
    align-items: center;
    justify-content: space-between;

    span {
      font-size: 0.300019rem;
      color: #666;
      font-weight: 600;
    }

    .el-dialog__headerbtn {
      position: static;

      .el-icon-close {
        color: #999;
        font-size: 15px;
      }
    }
  }
}

.dialog_div_new {
  width: 100%;
  height: 25vh;
  overflow: auto;
  background: #fff;

  .dialog_middle {
    width: 100%;
    height: calc(100% - 1.12507rem - 0.281268rem);
    box-sizing: border-box;
    padding: 0 0.843803rem 0 0.937559rem;
    margin-top: 0.281268rem;
    display: flex;
    align-items: center;
    overflow: auto;

    .svg_icon {
      width: 0.787549rem;
      height: 0.787549rem;
      margin-top: -0.093756rem;
    }

    div {
      width: calc(100% - 0.787549rem);
      margin-left: 0.468779rem;
      box-sizing: border-box;

      p {
        width: 100%;
        font-size: 0.262516rem;
        font-family: PingFang SC;
        font-weight: 400;
        line-height: 0.412526rem;
        color: #666666;
        margin: 0;
      }
    }
  }

  .dialog_bottom {
    width: 100%;
    height: 1.12507rem;
    box-sizing: border-box;
    padding: 0 0.187512rem;
    display: flex;
    align-items: center;
    justify-content: center;

    ::v-deep .el-button {
      width: 1.12507rem;
      height: 0.562535rem;
      text-align: center;
      box-sizing: border-box;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 0.225014rem;
      border-radius: 0.093756rem;
      background: #fff;
      border: 1px solid #dddddd;
      color: #666666;
    }

    ::v-deep .el-button--primary {
      background: #1890ff;
      border-color: #1890ff;
      color: #fff;
      margin-left: 0.112507rem;
    }

    ::v-deep .el-button--default {
      margin-right: 0.112507rem;
    }
  }


}
</style>